var baseUrl = "/proxyapi/" //开发
// var baseUrl = "http://183.207.103.103:443/"  //省中正式环境
// var baseUrl = "/mbtest/"; //省中医测试库
var sfUrl = "/sfproxy"
// var sfUrl = "http://183.207.103.103:8090/cas-app/" //省中正式环境
// var sfUrl = "http://test-wechat.jshtcm.com/apptest/cas-app/" //测试环境
var Rxports = {
    baseUrl: baseUrl,
  // 接口汇总
  // 通用ajax请求接口地址
  sfBaseUrl: "/pzx", //开发
  // sfBaseUrl:"http://183.207.103.103:8093", //省中正式
  // sfBaseUrl: "http://test-wechat.jshtcm.com/sf", //測試
  MENU_URL: function () {
    var menuurl = baseUrl + "coms-web/*.jsonRequest";
    return menuurl;
  },
  MENU_URL_SF: function () { //随访请求接口
    var menuurlsf = sfUrl + "/*.jsonRequest";
    return menuurlsf;
  },
  // 登录接口地址获取角色id
  ROLES_URL: function () {
    var rolesurl = baseUrl + "coms-web/logon/myRoles";
    return rolesurl;
  },

  // 登录接口地址
  LOGIN_URL: function () {
    var loginurl = baseUrl + "coms-web/logon/login";
    return loginurl;
  },
  //退出接口地址
  LOGOUT_URL: function () {
    var logout = baseUrl + "coms-web/logon/logout";
    return logout;
  },
  //修改密码接口地址
  CHANGEPWD_URL: function () {
    var changepwd = baseUrl + "coms-web/logon/changepwd";
    return changepwd;
  },
  // 图片上传接口地址
  IMGUPLOAD_URL: function () {
    var imgupload = baseUrl + "coms-web/admin/file/upload";
    return imgupload;
  },
  // 省市联动
  AREA_URL: function () {
    var area_url = baseUrl + "coms-web/coms.dictionary.coms_region.dic/";
    return area_url;
  },
  // 图片预览地址
  IMGVIEW_URL: function () {
    //var imgview_url = "http://115.236.19.147:15981/bs-coms/upload/";
    var imgview_url = "http://220.170.15.132:8083/bs-coms/upload/";
    // id=274 bg1.jpg id=275 bg2.jpg id=276 bg3.jpg
    // var imgview_url = "http://10.0.26.23:8071/coms-web/upload/"; //江辉电脑
    // var imgview_url = "http://10.0.26.61:8085/coms-web/upload/"; //朱雄敏电脑IP电脑

    return imgview_url;
  },
  // 文件下载地址
  FILEDOWNLOAD_URL: function () {
    //var filedownload_url = "http://115.236.19.147:15981/bs-coms/download/";
    var filedownload_url = "http://220.170.15.132:8083/bs-coms/download/";
    // var filedownload_url = "http://10.0.26.61:8085/coms-web/download/"; //朱雄敏电脑IP电脑

    return filedownload_url;
  },
  // 安卓二维码下载医生版
  APPDOWN_URL: function () {
    var appdown_url = "http://220.170.15.132:8083/bs-coms/coms-apk/coms-xiangtan-doctor.apk";
    // var appdown_url = "http://220.170.15.132:8083/bs-coms/coms-apk/coms-xiangtan.apk";
    return appdown_url;
  },
  // 安卓二维码下载患者版
  APPDOWN_URL2: function () {
    var appdown_url2 = "http://220.170.15.132:8083/bs-coms/coms-apk/coms-xiangtan.apk";
    // var appdown_url = "http://220.170.15.132:8083/bs-coms/coms-apk/coms-xiangtan.apk";

    return appdown_url2;
  },
  // 数据加载中
  loading: function (str) {
    var str = !str ? '数据加载中...' : str;
    var loadCon = $(" <div id='loading'><div class='bg_master'></div><div class='toast'><p class='weui_toast_content'>" + str + "</p></div></div>");
    $("body").append(loadCon);
  },
  // 获取请求令牌accessToken
  getsessionStorage: function (str) {
    return sessionStorage.getItem(str)
  },
  // 数据加载结束
  stoploading: function () {
    $("#loading").remove();
  },

  // 通用ajax请求方法
  commonajax: function (ServiceId, ServiceMethod, params) {　
    var dtd = $.Deferred(); //在函数内部，新建一个Deferred对象
    $.ajax({
      url: Rxports.MENU_URL(),
      type: "POST",
      headers: {
        "X-Access-Token": sessionStorage.getItem('accessToken'),
        "X-Service-Id": ServiceId,
        "X-Service-Method": ServiceMethod,
        "Content-Type": "application/json"
      },
      dataType: "JSON",
      data: params,
      beforeSend: function () {
        Rxports.loading("")
      },
      success: function (res) {
        Rxports.stoploading();
        dtd.resolve(res);
      },
      error: function (err) {
        Rxports.stoploading();
        return err
      }
    });
    return dtd.promise();
  },
  commonajaxSf: function (ServiceId, ServiceMethod, params) {
    var dtd = $.Deferred(); //在函数内部，新建一个Deferred对象
    $.ajax({
      url: Rxports.MENU_URL_SF(),
      type: "POST",
      headers: {
        "X-Access-Token": sessionStorage.getItem('sfToken'),
        "X-Service-Id": ServiceId,
        "X-Service-Method": ServiceMethod,
        "Content-Type": "application/json"
      },
      dataType: "JSON",
      data: params,
      beforeSend: function () {
        Rxports.loading("")
      },
      success: function (res) {
        Rxports.stoploading();
        dtd.resolve(res);
      },
      error: function (err) {
        Rxports.stoploading();
        return err
      }
    });
    return dtd.promise();
  },

  // 表单异步请求
  formajax: function (url, id) {
    var formData = new FormData($(id)[0]);
    console.log(formData);
    var dtd = $.Deferred(); //在函数内部，新建一个Deferred对象
    $.ajax({
      // url: '/view/advisory/add',
      url: url,
      type: 'POST',
      data: formData,
      async: true,
      cache: false,
      contentType: false,
      processData: false,
      beforeSend: function () {
        Rxports.loading()
      },
      success: function (res) {
        Rxports.stoploading()
        dtd.resolve(res);
      },
      error: function (xhr, type) {
        Rxports.stoploading()
      }
    })
    return dtd.promise();
  },
  // 获取url的字符串参数
  getParameter: function (param) {
    var query = window.location.search;
    var iLen = param.length;
    var iStart = query.indexOf(param);
    if (iStart == -1)
      return "";
    iStart += iLen + 1;
    var iEnd = query.indexOf("&", iStart);
    if (iEnd == -1) {
      return query.substring(iStart);
    }

    return query.substring(iStart, iEnd);
  },
  // 获取字符串参数
  // getstrParameter: function(query, param) {
  //     var iLen = param.length;
  //     var iStart = query.indexOf(param);
  //     if (iStart == -1)
  //         return "";
  //     iStart += iLen + 1;
  //     var iEnd = query.indexOf("&", iStart);
  //     if (iEnd == -1) {
  //         return query.substring(iStart);
  //     }

  //     return query.substring(iStart, iEnd);
  // },
  // 时间格式化
  formatDate: function (date, fmt) {
    let padLeftZero = str => ('00' + str).substr(str.length)
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    let o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    }
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k] + ''
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
      }
    }
    return fmt
  },
  ajaxPrompt: function (str) {
    var html_con = $("<div id='ajaxPrompt'><div class='bg_master1'></div><div class='toast toast1'><p class='weui_toast_content'>" + str + "</p></div></div>");
    $("body").append(html_con);
    setTimeout(function () {
      $("#ajaxPrompt").remove();
    }, 2000);

  },
  // confirmPrompt: function(str) {
  //     var html_con = $("<div id='confirmPrompt'><div class='bg_master1'></div><div class='toast3'><p>" + str + "</p><p> <button class='btn btn-success' id='savedata'>保存</button><button type='button' class='btn btn-danger' id='cancledata'>取消</button></p></div></div>");
  //     $("body").append(html_con);
  // },
  // 验证表单的字数
  gbcount: function (message, total, used, remain) {

    var max;
    max = total.value;

    if (message.value.length > max) {

      message.value = message.value.substring(0, max);

      used.value = max;

      remain.value = 0;
      alert("不能超过150个字!");
    } else {

      used.value = message.value.length;

      remain.value = max - used.value;

    }

  },
  // 数组去重复
  arrunique: function (arr, str) {
    for (var i = 0, temp = {}, result = [], ci; ci = arr[i++];) {
      var ordid = ci[str];
      if (temp[ordid]) {
        continue;
      }
      temp[ordid] = true;
      result.push(ci);
    }
    return result

  },
  // 通过身份证获取生日
  getBirthdatByIdNo: function (iIdNo) {
    var tmpStr = "";
    iIdNo = $.trim(iIdNo);
    if (iIdNo.length == 0) {
      return tmpStr
    }
    if (iIdNo.length == 15) {
      tmpStr = iIdNo.substring(6, 12);
      tmpStr = "19" + tmpStr;
      tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)
      return tmpStr;
    } else {
      tmpStr = iIdNo.substring(6, 14);
      tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)
      return tmpStr;
    }
  },
  // 通过身份证获取性别
  getSexByIdNo: function (iIdNo) {
    var tmpStr = "";
    iIdNo = $.trim(iIdNo);
    if (iIdNo.length == 0) {
      return tmpStr
    }
    if (iIdNo.length == 15) {
      tmpStr = iIdNo.substring(14);

    } else {
      tmpStr = iIdNo.substring(16, 17);
    }
    if (parseInt(tmpStr) % 2 == 0) {
      return 2;
    } else {
      return 1
    }
  }
}
module.exports = Rxports;
// 阻止回车键提交表单
document.onkeydown = function (event) {
  var target, code, tag;
  if (!event) {
    event = window.event; //针对ie浏览器
    target = event.srcElement;
    code = event.keyCode;
    if (code == 13) {
      tag = target.tagName;
      if (tag == "TEXTAREA") {
        return true;
      } else {
        return false;
      }
    }
  } else {
    target = event.target; //针对遵循w3c标准的浏览器，如Firefox
    code = event.keyCode;
    if (code == 13) {
      tag = target.tagName;
      if (tag == "INPUT") {
        return false;
      } else {
        return true;
      }
    }
  }
}
